<template>
  <div class="replace-box">
    <el-dialog width="500px" :title="dialogTitle" v-model="dialogFormVisible" :draggable="true" @close="dialogClose">
      <div>
        <el-form ref="dialogFormRef" :model="dialogForm" :rules="dialogFormRules">
          <el-row>
            <el-col :span="24">
              <el-form-item prop="tjName" label="推荐人姓名">
                <el-input v-model="dialogForm.tjName" placeholder="请输入推荐人姓名" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item prop="tjPhone" label="联系方式">
                <el-input v-model="dialogForm.tjPhone" placeholder="请输入推荐人联系方式" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-divider border-style="dashed" />
          <el-row>
            <el-col :span="24">
              <el-form-item prop="tjRemark" label="备注">
                <el-input v-model="dialogForm.tjRemark" :autosize="{ minRows: 2 }" type="textarea" placeholder="备注..." />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="submitHandle">
            确定
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { reactive, ref, toRefs } from 'vue'
import hexMD5 from "@/utils/md5.min.js"
import { numFormat, successDeal, warningDeal } from '@/utils/utils'
import api from '../api'
export default {
  setup(props, context) {
    const dialogFormRef = ref(null)

    const state = reactive({
      dialogTitle: '新增推荐人',
      dialogType: 1,
      dialogFormVisible: false,
      dialogForm: {},
      dialogFormRules: {
        tjName: [
          { required: true, message: '请输入推荐人姓名', trigger: 'blur' },
        ],
        tjPhone: [
          { required: false, message: '请输入推荐人联系方式', trigger: 'blur' },
        ],
      },
    })

    function dialogShow(type = 1, data = {}) {
      state.dialogType = type
      state.dialogForm = JSON.parse(JSON.stringify(data))
      if (type == 1) {
        state.dialogTitle = '新增推荐人'
      } else {
        state.dialogTitle = '编辑推荐人'
      }
      state.dialogFormVisible = true
    }

    function submitHandle() {
      dialogFormRef.value.validate((valid, fields) => {
        if (valid) {
          //校验通过
          const obj = {
            ...state.dialogForm
          }
          let apiName = (state.dialogType == 1) ? 'accountAdd' : 'accountModify'
          api[apiName](obj).then(res => {
            if (res.code == '200') {
              successDeal(res.msg)
              context.emit('dialogClose')
              state.dialogFormVisible = false
            } else {
              warningDeal(res.msg)
            }
          }).catch(err => {
            console.log(err)
          })
        }
      })
    }

    function dialogClose() {
      dialogFormRef.value.resetFields()
    }

    return {
      ...toRefs(state),
      dialogFormRef,
      dialogClose,
      submitHandle,
      dialogShow,
      numFormat,
    }
  }
}
</script>

<style lang="scss" scoped>
:deep(.el-divider--horizontal) {
  margin: 5px 0 !important;
}
:deep(.el-input-group__append) {
  padding: 0 20px !important;
}
</style>